package com.example.demo.springcycle.createbean;

import com.example.demo.springcycle.entity.Dog;

import java.lang.reflect.Field;

/**
 * 不用new，使用反射技术生成dog对象，并设置属性值
 */
public class DogReflect {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        //获取Dog类
        final Class<?> dogClass = Class.forName("com.example.demo.springcycle.entity.Dog");
        //通过类生成对象
        final Dog dog = (Dog)dogClass.newInstance();
        //获取特定的属性
        final Field field = dogClass.getField("age");
        field.setAccessible(true);
        //填充属性值
        field.set(dog,1);
        dog.say();
    }

}
